﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;
using System.Globalization;

namespace Travel.Api.Site
{
    public partial class OptionChoiceViewPage : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }

        }

        protected void grid_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PackageDestinationOption item = (PackageDestinationOption)e.Item.DataItem;
                               
                Label lbl = (Label)e.Item.FindControl("lblOptionChoice");
                lbl.Text = item.Choices.Where(i => i.ChoiceId == item.SelectedChoiceId).Single().ClientChoiceDescription;
            }
        }


        public override void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                var clientPackage = repo.FindClientPackage(TravelId);
                lblTravelDate.Text = (clientPackage.StartDate.HasValue ? clientPackage.StartDate.Value.ToString("d") : "") +
                    " - " + (clientPackage.EndDate.HasValue ? clientPackage.EndDate.Value.ToString("d") : "");
                var options = repo.FindOptions(PkgDestId, TravelId);
                grid.DataSource = options.Where(i => i.SelectedChoiceId != -1);
                grid.DataBind();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Client/TravelPackage.aspx");
        }

        public int PkgDestId
        {
            get
            {
                int result = -1;
                if (!string.IsNullOrEmpty(Request.QueryString["pdid"]))
                    result = Convert.ToInt32(Request.QueryString["pdid"]);
                return result;
            }
        }

        public int TravelId
        {
            get
            {
                int result = -1;
                if (!string.IsNullOrEmpty(Request.QueryString["tid"]))
                    result = Convert.ToInt32(Request.QueryString["tid"]);
                return result;
            }
        }
    }
}